package com.demostic.demostic_test.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.accloud.service.ACUserDevice;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

public class SpUtils {
    public static final String FILE_NAME = "share_data";

    public static void put(Context context, String key, Object object) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, 0).edit();
        if (object instanceof String) {
            editor.putString(key, (String) object);
        } else if (object instanceof Integer) {
            editor.putInt(key, ((Integer) object).intValue());
        } else if (object instanceof Boolean) {
            editor.putBoolean(key, ((Boolean) object).booleanValue());
        } else if (object instanceof Float) {
            editor.putFloat(key, ((Float) object).floatValue());
        } else if (object instanceof Long) {
            editor.putLong(key, ((Long) object).longValue());
        } else {
            editor.putString(key, object.toString());
        }
        SharedPreferencesCompat.apply(editor);
    }

    public static Object get(Context context, String key, Object defaultObject) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, 0);
        if (defaultObject instanceof String) {
            return sp.getString(key, (String) defaultObject);
        }
        if (defaultObject instanceof Integer) {
            return Integer.valueOf(sp.getInt(key, ((Integer) defaultObject).intValue()));
        }
        if (defaultObject instanceof Boolean) {
            return Boolean.valueOf(sp.getBoolean(key, ((Boolean) defaultObject).booleanValue()));
        }
        if (defaultObject instanceof Float) {
            return Float.valueOf(sp.getFloat(key, ((Float) defaultObject).floatValue()));
        }
        if (defaultObject instanceof Long) {
            return Long.valueOf(sp.getLong(key, ((Long) defaultObject).longValue()));
        }
        return null;
    }

    public static void remove(Context context, String key) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, 0).edit();
        editor.remove(key);
        SharedPreferencesCompat.apply(editor);
    }

    public static void clear(Context context) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, 0).edit();
        editor.clear();
        SharedPreferencesCompat.apply(editor);
    }

    public static boolean contains(Context context, String key) {
        return context.getSharedPreferences(FILE_NAME, 0).contains(key);
    }

    public static Map<String, ?> getAll(Context context) {
        return context.getSharedPreferences(FILE_NAME, 0).getAll();
    }

    private static class SharedPreferencesCompat {
        private static final Method sApplyMethod = findApplyMethod();

        private SharedPreferencesCompat() {
        }

        private static Method findApplyMethod() {
            try {
                return SharedPreferences.Editor.class.getMethod("apply", new Class[0]);
            } catch (NoSuchMethodException e) {
                return null;
            }
        }

        public static void apply(SharedPreferences.Editor editor) {
            try {
                if (sApplyMethod != null) {
                    sApplyMethod.invoke(editor, new Object[0]);
                    return;
                }
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            }
            editor.commit();
        }
    }

    public static boolean saveArray(Context context, List<ACUserDevice> list) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, 0).edit();
        for (int i = 0; i < list.size(); i++) {
            editor.remove("Status_" + i);
            editor.putString("Status_" + i, list.get(i).getName() + "##" + list.get(i).getOwner() + "##" + list.get(i).getDeviceId() + "##" + list.get(i).getPhysicalDeviceId());
        }
        editor.putInt("Status_size", list.size());
        return editor.commit();
    }

    public static void loadArray(Context context, List<String> list) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, 0);
        list.clear();
        int size = sp.getInt("Status_size", 0);
        for (int i = 0; i < size; i++) {
            list.add(sp.getString("Status_" + i, null));
        }
    }

    public static boolean saveStringArray(Context context, List<String> list) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, 0).edit();
        for (int i = 0; i < list.size(); i++) {
            editor.remove("Errors_" + i);
            editor.putString("Errors_" + i, list.get(i));
        }
        editor.putInt("Error_size", list.size());
        return editor.commit();
    }

    public static void loadStringArray(Context context, List<String> list) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, 0);
        list.clear();
        int size = sp.getInt("Error_size", 0);
        for (int i = 0; i < size; i++) {
            list.add(sp.getString("Errors_" + i, null));
        }
    }

    public static boolean saveInt(Context context, String key, int value) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, 0).edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    public static int getInt(Context context, String key) {
        return context.getSharedPreferences(FILE_NAME, 0).getInt(key, 100);
    }

    public static boolean saveFloat(Context context, String key, float value) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, 0).edit();
        editor.putFloat(key, value);
        return editor.commit();
    }

    public static float getSpFloat(Context context, String key) {
        return context.getSharedPreferences(FILE_NAME, 0).getFloat(key, 0.0f);
    }

    public static boolean saveString(Context context, String key, String value) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, 0).edit();
        editor.putString(key, value);
        return editor.commit();
    }

    public static String getSpString(Context context, String key) {
        return context.getSharedPreferences(FILE_NAME, 0).getString(key, "");
    }

    public static boolean saveLong(Context context, String key, long value) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, 0).edit();
        editor.putLong(key, value);
        return editor.commit();
    }

    public static long getLong(Context context, String key) {
        return context.getSharedPreferences(FILE_NAME, 0).getLong(key, 0);
    }

    public static boolean saveBoolean(Context context, String key, boolean value) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, 0).edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }

    public static boolean getBoolean(Context context, String key) {
        return context.getSharedPreferences(FILE_NAME, 0).getBoolean(key, false);
    }

    public static boolean saveSubdomain(Context context, String value) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, 0).edit();
        editor.putString("subdomain", value);
        return editor.commit();
    }

    public static String getSubdomain(Context context) {
        return context.getSharedPreferences(FILE_NAME, 0).getString("subdomain", "");
    }
}
